package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class BannerServiceImpl implements IBannerService {
    @Autowired
    private BannerRepository bannerRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Banner banner) {
        banner.setId(null);
        bannerRepository.save(banner);
    }



    //查询单个目的地对象
    @Override
    public Banner get(String id) {
        Banner byId = bannerRepository.findById(id).orElse(null);
        //强转
        return byId;//如果是空是就返回空值,否则返回对象
    }

    @Override
    public List<Banner> list() {
        List<Banner> banners = bannerRepository.findAll();
        return banners;
    }

    @Override
    public void delete(String id) {
        bannerRepository.deleteById(id);
    }

    //修改操作
    @Override
    public void update(Banner banner) {
        //
        bannerRepository.save(banner);


    }





    @Override
    public Page<Banner> query(BannerQuery qo) {
        Query query = new Query();

        long rows = mongoTemplate.count(query, Banner.class);
        if(rows==0){
           Page.empty();
        }


        Pageable pageable= PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "sequence");
        query.with(pageable);
        List<Banner> banners = mongoTemplate.find(query, Banner.class);
        //结果集合,分页
        return new PageImpl<Banner>(banners,pageable,rows);
    }
    //查询首页的轮播图(前5个banner对象的封面字段)
    @Override
    public List<Banner> queryBanner(int type) {
        //分页对象
        Pageable pageable=PageRequest.of(0,5, Sort.Direction.ASC,"sequence");
        //查询类型是游记类型的并且状态是正常(没有禁用的)banner对象集合,同时分页和按序号排序(目的是给后台管理员使用序号控制谁排在前面的)
        List<Banner> banners = bannerRepository.findByTypeAndState(type, Banner.STATE_NORMAL, pageable);
        return banners;//前端页面要获取的是banners集合,直接返回它,不是page对象
    }


    @Override
    public void saveOrUpdate(Banner banner) {
        if (!StringUtils.hasLength(banner.getId())) {
            banner.setId(null);
            this.save(banner);
        }else{
            this.update(banner);
        }
    }

    @Override
    public List<Banner> queryBannerStrategy(int type) {

        return bannerRepository.findAll();
    }


}
